/*
Kiwi Editor is a framework for building WYSIWYG web editors.
Copyright (C) 2011  Roosevelt de Lima Sardinha

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Contact the author at roosevelt1@gmail.com (Roosevelt) 
*/

/* Textbox EO
 This class represents an editor object containing a simple textbox. When placed in an edition area, you can change its properties.
 */
function textboxEO(){	
    this.showProperties = function(){
	   this.tabId = this.propertyBox.addTab("Textbox");
   	   this.propertyBox.addProperty(this.tabId, 0, 0, 'name', 'Textbox', this, {
	   		label: 'Name',
			help: 'Sets an identifier of the textbox.'
	   });
	    
	   this.propertyBox.addProperty(this.tabId, 1, 0, 'value', 'Textbox', this, {
	   		label: 'Content',
			help: 'Sets the content of the textbox.'
	   });
   
	   this.propertyBox.addProperty(this.tabId, 2, 0, 'width', 'Textbox', this, {
	   		label: 'Width',
			help: 'Sets the width of the textbox.'
	   });
	   
	   this.propertyBox.addProperty(this.tabId, 3, 0, 'left', 'Textbox', this, {
	   		label: 'Left',
			help: 'Sets the left coordinate of the textbox.'
	   });

	   this.propertyBox.addProperty(this.tabId, 0, 2, 'top', 'Textbox', this, {
	   		label: 'Top',
			help: 'Sets the top coordinate of the textbox.'
	   });		

	   this.propertyBox.addProperty(this.tabId, 1, 2, 'maxlength', 'Textbox', this, {
	   		label: 'Length',
			help: 'Sets the maximum number of characters in the textbox.'
	   });		

	   this.propertyBox.addProperty(this.tabId, 2, 2, 'bgcolor', 'Color', this, {
	   		label: 'Background Color',
			help: 'Sets the background color of the textbox.'
	   });	
				  
 	   this.propertyBox.addProperty(this.tabId, 3, 2, 'fontcolor', 'Color', this, {
	   		label: 'Font Color',
			help: 'Sets the color of the font.'
	   });	

 	   this.propertyBox.addProperty(this.tabId, 0, 4, 'font', 'Font', this, {
	   		label: 'Font',
			help: 'Sets the color of the font.'
	   });	
	   
   	   this.propertyBox.addProperty(this.tabId, 1, 4, 'fontsize', 'Textbox', this, {
	   		label: 'Font Size',
			help: 'Sets the size of the font.'
	   });
    };
    
    this.setProperty = function(property, value){
        // Changes the property of  this object setting it to value.
        switch (property) {
			case 'name':
				this.textbox.name = value;
				break;
            case 'value':
                this.textbox.value = value;
                break;
            case 'width':
                if (parseInt(value) < document.getElementById(this.EOM.editionArea.parentId).offsetWidth) {
                    this.textbox.style.width = value;
                    this.EOM.correctPosition(this.id);
                }
                break;
            case 'height':
                this.textbox.style.height = value;
                this.EOM.correctPosition(this.id);
                break;
            case 'maxlength':
                this.textbox.maxlength = value;
                break;
            case 'bgcolor':
                this.textbox.style.backgroundColor = value;
                break;
            case 'fontcolor':
                this.textbox.style.color = value;
                break;
            case 'font':
                this.textbox.style.fontFamily = value;
                break;
            case 'fontsize':
                this.textbox.style.fontSize = value;
                break;
            case 'content':
                this.textbox.value = value;
                break;
            default:
                break;
        }
    };
    
    this.getProperty = function(property){
        // Gets the property of  this object
        switch (property) {
			case 'name':
				return this.textbox.name;
				break;
            case 'value':
                return this.textbox.value;
                break;
            case 'width':
                return this.getWidth();
                break;
            case 'height':
                return this.getHeight();
                break;
            case 'maxlength':
                return this.textbox.maxlength || 10;
                break;
            case 'bgcolor':
                return this.textbox.style.backgroundColor;
                break;
            case 'fontcolor':
                return this.textbox.style.color || '#000000';
                break;
            case 'fontsize':
                return this.textbox.style.fontSize || '12px';
                break;
            case 'font':
                return this.textbox.style.fontFamily || 'Arial';
                break;
			case 'propertyBox':
				return this.propertyBox.id;
				break;
			case 'instructionBox':
				return this.instructionBox.id;
				break;
            default:
                break;
        }
    };
    
    this.JSON = function(){
        var EO_JSON = {
			name: this.getProperty('name'),
            content: this.getProperty('value'),
            width: this.getProperty('width'),
            left: this.getProperty('left'),
            top: this.getProperty('top'),
            length: this.getProperty('maxlength'),
            bgcolor: this.getProperty('bgcolor'),
            fontcolor: this.getProperty('fontcolor'),
            fontsize: this.getProperty('fontsize'),
            font: this.getProperty('font'),
        };
        return EO_JSON;
    }
	
	this.HTML = function(){
		var html = "<input type=\"textbox\" name=\"" + this.getProperty('name') + "\" value=\""+ this.getProperty('value')+ "\" " +
		"style=\"width:"+ this.getProperty('width') + "px; top: " + this.getProperty('top') + "px; left:"+ this.getProperty('left') + "px;  " +
		"color:"+ this.getProperty('fontcolor') + "; font-size:"+ this.getProperty('fontsize') + "; font-family:"+ this.getProperty('font') + "; "+
		"background-color:"+ this.getProperty('bgcolor') + "; " +
		"position: absolute;\" "+
		"maxlength=\""+this.getProperty('maxlength')+ "\" "+
		"/>";
		return html;
	}
    
    this.help = "A simple textbox for writing some text. Usually found in forms. You can change its properties in the Property Box below.";
	
	this.objectList = new Array();
	this.selectorList = new Array();
		
    this.init = function(){        
        var objectList = new Array;
        objectList.push('textbox');
        this.objectList = objectList;
        
        var textbox = document.createElement('input');
        textbox.type = 'text';
        textbox.readOnly = true;
        textbox.value = 'Textbox';
		textbox.style.cursor = 'move';
        this.textbox = textbox;
		
		var selectorList = new Array;
		selectorList.push('textbox');
		this.selectorList = selectorList; 	
    }
}
